// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
  setIsTop: (isTop) => ipcRenderer.send('set-is-top', isTop),
  exitApp: () => ipcRenderer.send('exit-app'),
  setAppConfig: (content) => ipcRenderer.send('set-app-config', content),
  setAuto: (content) => ipcRenderer.send('set-auto', content),
  getAllTodo: () => ipcRenderer.invoke('getTodo'),
  getAppConfig: () => ipcRenderer.invoke('getConfig'),
  updateTodo: (data) => ipcRenderer.send('updateTodo', data),
  setTodo: (item) => ipcRenderer.send('setTodo', item),
  deleteTodo: (key) => ipcRenderer.send('deleteTodo', key)
});



contextBridge.exposeInMainWorld('electronAPI', {
  minimizeWindow: () => ipcRenderer.send('window-control', 'minimize'),
  toggleMaximizeWindow: () => ipcRenderer.send('window-control', 'toggle-maximize'),
  closeWindow: () => ipcRenderer.send('window-control', 'close'),
  isWindowMaximized: () => ipcRenderer.invoke('window-get-state'),
  onWindowStateUpdate: (callback) => {
    ipcRenderer.on('window-state-changed', (_, state) => callback(state))
  },
  removeWindowStateListeners: () => {
    ipcRenderer.removeAllListeners('window-state-changed')
  }
})

// preload.js
contextBridge.exposeInMainWorld('electron', {
  fs: require('fs'),
  path: require('path')
});

contextBridge.exposeInMainWorld('electron', {
  lowdb: {
    read: () => ipcRenderer.invoke('lowdb-read'),
    write: (data) => ipcRenderer.send('lowdb-write', data)
  }
});

contextBridge.exposeInMainWorld('ipc', {
  invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
  send: (channel, ...args) => ipcRenderer.send(channel, ...args),
  on: (channel, listener) => ipcRenderer.on(channel, listener)
});